<?php
defined( '_JEXEC' ) or die( 'Restricted access' );

class igratingModelIgrating extends igbaseModel
{
    protected $context = 'igrating';
	protected $tablename = 'igallery_rating';
	protected $filterOrderDefault = 'date';
	protected $reorderClause = '';
	
	function __construct($task)
	{
		parent::__construct();
    }
	
	function _buildQuery()
	{
		$where		= $this->_buildContentWhere();
		$orderby	= $this->_buildContentOrderBy();
        $query = ' SELECT '.
		'#__igallery_rating.*, '.
		'#__igallery.name, '.
		'#__igallery_img.filename, #__igallery_img.alt_text '.
    	' FROM #__igallery_rating '.
    	' INNER JOIN #__igallery ON #__igallery.id = #__igallery_rating.gallery_id '.
    	' INNER JOIN #__igallery_img ON #__igallery_img.id = #__igallery_rating.img_id '.
    	$where . $orderby;
        return $query;
	}
	
	function _buildContentWhere()
	{
		global $mainframe, $option;
		
		$filter_state		= $mainframe->getUserStateFromRequest( $this->context.'.filter_state',		'filter_state',		'',				'word' );
		$filter_catid		= $mainframe->getUserStateFromRequest( $this->context.'.filter_catid',		    'filter_catid',		0,				'int' );
		$filter_order		= $mainframe->getUserStateFromRequest( $this->context.'.filter_order',		'filter_order',		'ordering',	'cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( $this->context.'.filter_order_Dir',	'filter_order_Dir',	'',				'word' );

		$where = array();
        
		if ($filter_catid > 0)
		{
			$where[] = '#__igallery_rating.gallery_id = '.(int) $filter_catid;
		}
		if ( $filter_state ) 
		{
			if ( $filter_state == 'P' ) 
			{
				$where[] = '#__igallery_rating.published = 1';
			} 
			else if ($filter_state == 'U' ) 
			{
				$where[] = '#__igallery_rating.published = 0';
			}
		}

		$where 		= ( count( $where ) ? ' WHERE '. implode( ' AND ', $where ) : '' );

		return $where;
	}
}